package com.code2roc.fastface.util;

import com.code2roc.fastface.db.CommonDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DataBaseUtil {
    @Autowired
    private CommonDTO commonDTO;

    public void init() {
        String ddlSQL = "";
        if (!checkTableExist("FaceRegist")) {
            ddlSQL = "CREATE TABLE FaceRegist (" +
                    "ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                    "UserID TEXT ," +
                    "UserName TEXT ," +
                    "RegistDate TEXT," +
                    "UpdateDate TEXT," +
                    "RegistIndex TEXT," +
                    "Base64Image TEXT," +
                    "ExtendA TEXT," +
                    "ExtendB TEXT," +
                    "ExtendC TEXT," +
                    "ExtendD TEXT)";
            commonDTO.executeSQL(ddlSQL, null);
        }
    }

    private boolean checkTableExist(String tableName) {
        boolean check = false;
        String checkSQL = "";
        checkSQL = "SELECT COUNT(*) FROM sqlite_master  WHERE name ='" + tableName + "' and type='table'";
        check = ConvertOp.convert2Int(commonDTO.executeSQLToQuery(checkSQL, null)) > 0;
        return check;
    }
}